#!/usr/bin/env python
# -*- coding:UTF-8 -*-
__author__ = 'wycao'


import requests
import json,time
from jiebatitle.jbtitle import JbTitle
from pgre.postgkeyword import PostKeyword

class RequestCninfo:
    def __init__(self):
        self.jb = JbTitle()
        requests.get(url='http://www.cninfo.com.cn/cninfo-new/disclosure/szse')
        self.pg = PostKeyword()

    def get_info(self,category,page=1):
        url = r'http://www.cninfo.com.cn/cninfo-new/announcement/query'
        category_dict = {"category_ndbg_szsh": "年度报告",
                         "category_bndbg_szsh": "半年度报告",
                         "category_yjdbg_szsh": "一季度报告",
                         "category_sjdbg_szsh": "三季度报告"}
        formdata ={"stock":"",
                    "searchkey":"",
                    "plate":"",
                    "category":category,
                    "trade":"",
                    "column":"szse",
                    "columnTitle":u"历史公告查询",
                    "pageNum":page,
                    "pageSize":"30",
                    "tabName":"fulltext",
                    "sortName":"",
                    "sortType":"",
                    "limit":"",
                    "showTitle":category+"/category/"+category_dict[category],
                    "seDate":"请选择日期"}
        try:
            #发起请求
            rsp = requests.post(url=url,data=formdata)
            if rsp.status_code == 200:
                # 如果请求结果状态为200
                return rsp.text
            else:
                # 如果请求结果状态不是200,等待10秒,再请求
                time.sleep(31)
                rsp2 = requests.post(url=url, data=formdata)
                if rsp2.status_code == 200:
                    return rsp2.text
                else:
                    print(rsp2.text)
                    return False
        except BaseException as e:
            print(e)



    def process_json(self,category):
        page =1   #category_bndbg_szsh类型,第170页
        result = self.get_info(category,page)
        jsoninfo = json.loads(result)
        total = jsoninfo['totalAnnouncement']
        pagenum = int(total / 30) +1
        self.insert_date(category=category,jsoninfo=jsoninfo)
        try:
            for p in range(page+1,pagenum+2):
                time.sleep(2)
                result = self.get_info(category, p+2)
                jsoninfo = json.loads(result)
                self.insert_date(category=category,jsoninfo=jsoninfo)
                print('{0}类型,第{1}页,共{2}条入库成功'.format(category,p+2,(p+2)*30))
        except BaseException as e:
            print(e)
        finally:
            self.pg.conn.close()
            print("数据库链接关闭成功")


    def insert_date(self,category,jsoninfo):
        announcements = jsoninfo['announcements']
        try:
            for a in range(0,len(announcements)):
                cmp_code = announcements[a]['secCode']
                cmp_short_name = announcements[a]['secName']
                title = announcements[a]['announcementTitle']
                jcturl = announcements[a]['adjunctUrl']
                annount_date = announcements[a]['announcementTime']
                jct_list = jcturl.split("/")
                urlid_list = jct_list[len(jct_list)-1].split(".")
                report_href = r"http://www.cninfo.com.cn/cninfo-new/disclosure/szse/bulletin_detail/true/"+str(urlid_list[0])+"?announceTime="+str(jct_list[len(jct_list)-2])
                an_date = time.localtime(annount_date/1000)
                show_date = time.strftime("%Y-%m-%d",an_date)
                dataexist = self.verify_exist(category=category,companycode=cmp_code,reportname=title.rstrip(),showdate=show_date)
                if dataexist:
                    values = []
                    values.append(category)
                    if category == 'category_ndbg_szsh':
                        values.append('年度报告')
                    elif category == 'category_bndbg_szsh':
                        values.append('半年度报告')
                    elif category =='category_yjdbg_szsh':
                        values.append('一季度报告')
                    elif category == 'category_sjdbg_szsh':
                        values.append('三季度报告')
                    else:
                        pass
                    values.append(cmp_code.rstrip())
                    values.append(cmp_short_name.rstrip())
                    values.append(title.rstrip())
                    values.append(report_href)
                    values.append(urlid_list[1].rstrip())
                    values.append(show_date)
                    title_list = self.jb.cut_text(title.rstrip())
                    porttype_list = ['半年度报告','年度报告','第一季报告','第三季度报告']
                    for tl in porttype_list:
                        if tl in title_list:
                            tlindex = title_list.index(tl)
                            ptdate_list = title_list[:tlindex]
                            rpt_date = "".join(ptdate_list)
                            port_type = tl
                            values.append(rpt_date)
                            values.append(port_type)
                            if len(title_list) > tlindex:
                                portstatus = title_list[-3]
                                values.append(portstatus)
                                self.pg.insert_data(values=values)
                            else:
                                portstatus =""
                                values.append(portstatus)
                                self.pg.insert_data(values=values)
                else:
                    pass

        except BaseException as e:
            print(e)


    def verify_exist(self,category,companycode,reportname,showdate):
        """
        根据 一些参数值, 判断,数据库中是否已经存在
        :param category: 分类编码
        :param companycode: 企业编码
        :param reportname: 报告名称
        :param showdate: 展示日期
        :return: 
        """
        result = self.pg.select_count_date(values=[category,companycode,reportname,showdate])
        if int(result) >= 1:
            #存在,返回 false
            return False
        else:
            #不存在 返回True
            return True